package com.ah.shanxi_demo.sync.client;

import com.ah.shanxi_demo.common.enums.SXPortEnum;
import com.ah.http.httpsclient.MyHttpsClient;
import com.ah.http.httpsclient.constants.HttpHeaderContentTypeEnum;
import com.ah.shanxi_demo.sync.param.PolicyDataReportPO;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;

import static com.ah.shanxi_demo.sync.convert.ConvertSyncParam.convert_sync_policy_data;
import static com.ah.shanxi_demo.sync.convert.ConvertSyncRequest.convert_sync_request_header;
import static com.ah.shanxi_demo.sync.utils.syncResultConvertObject.sync_result_policy_data;

/**
 * 策略数据采集
 * @author zt 2020/8/26
 */
public class SyncPolicyDataReportClient {
    private static final Logger log = LoggerFactory.getLogger(SyncPolicyDataReportClient.class);

    /**
     * 部级平台 策略数据采集 客户端
     * @param
     * @return
     */
    public static Boolean policy_data_client(PolicyDataReportPO policyDataReportPO, String serviceAddress) {

        //部级平台进行同步信息
        String policy_serviceAddress = serviceAddress+ SXPortEnum.DATE_REPORT.getKey();
        Object responseObject = sdk_policyData(policyDataReportPO, policy_serviceAddress);

        //解析返回值 返回同步表模型
        return sync_result_policy_data(responseObject);
    }

    /**
     * 策略数据采集接口_SDK
     *
     * @param policyDataReportPO
     * @param sync_serviceAddress
     * @return
     */
    public static Object sdk_policyData(PolicyDataReportPO policyDataReportPO, String sync_serviceAddress) {

        //地址
        String url = sync_serviceAddress;

        //参数
        Map<String, Object> paramsMap = convert_sync_policy_data(policyDataReportPO);

        //请求头
        Map<String, String> headerMap = convert_sync_request_header(paramsMap);

        try {

            log.info("sync_policy【url】：" + JSON.toJSONString(url));
            log.info("sync_policy【paramsMap】：" + JSON.toJSONString(paramsMap));
            log.info("sync_policy【headerMap】：" + JSON.toJSONString(headerMap));

            Object responseObject = MyHttpsClient.sendPostHttps(url, paramsMap, headerMap, HttpHeaderContentTypeEnum.JSON.getKey());

            log.info("sync_policy【responseObject】：" + JSON.toJSONString(responseObject));
            return responseObject;
        } catch (Exception e) {
            log.error("sync_policy【Exception】：" + JSON.toJSONString(e));
        }

        return null;
    }
}
